package cc.comac.mom.datahandlers.entity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JSpinner.DateEditor;

import cc.comac.mom.dao.entity.QualityAttachmentDao;
import cc.comac.mom.entity.QualityAttachmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAsrCheckCorrigendumExplain;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.dao.entity.QualityAsrCheckCorrigendumExplainDao;
import cc.comac.mom.entity.QualityAsrCheckCorrigendumExplainEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;

@Component
@Transactional
public class QualityAsrCheckCorrigendumExplainDataHandler
		extends ServiceImpl<QualityAsrCheckCorrigendumExplainDao, QualityAsrCheckCorrigendumExplainEntity> {

	@Autowired
	private QualityAsrCheckCorrigendumExplainDao qualityAsrCheckCorrigendumExplainDao;
	@Autowired
	private QualityAttachmentDao qualityAttachmentDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private OSSService oSSService;
	
	@Value("${upload.tempDir}")
	private String rootPath;

	// 通过检验表单主键查询勘误数据
	public List<QualityAsrCheckCorrigendumExplainEntity> selectByFormId(long formId) {
		QueryWrapper<QualityAsrCheckCorrigendumExplainEntity> queryWrapper = new QueryWrapper<QualityAsrCheckCorrigendumExplainEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_check_form_id", formId);
		List<QualityAsrCheckCorrigendumExplainEntity> selectList = qualityAsrCheckCorrigendumExplainDao
				.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0){
			for(QualityAsrCheckCorrigendumExplainEntity en:selectList){
				QualityAttachmentEntity att=qualityAttachmentDao.selectById(en.getAttachmentId());
				en.setCorrigendumName(att.getRealName());
				en.setCorrigendumPath(att.getFilePath());
			}
		}
		return selectList;
	}

	// 勘误说明上传
	public ResultBean saveCheckCorrigendumExplainUpload(QualityAsrCheckCorrigendumExplain qualityAsrCheckCorrigendumExplain) throws Exception {
		ResultBean resultBean = new ResultBean(true);
		ArrayList<QualityAsrCheckCorrigendumExplainEntity> corrigendumExplainList = new ArrayList<>();
		if(qualityAsrCheckCorrigendumExplain.getAttachmentIds()!=null&&qualityAsrCheckCorrigendumExplain.getAttachmentIds().size()>0) {
			for (Long attId : qualityAsrCheckCorrigendumExplain.getAttachmentIds()) {
				QualityAsrCheckCorrigendumExplainEntity qualityAsrCheckCorrigendumExplainEntity = new QualityAsrCheckCorrigendumExplainEntity();
				qualityAsrCheckCorrigendumExplainEntity.setAsrCheckFormId(qualityAsrCheckCorrigendumExplain.getInnerData().getAsrCheckFormId());
				qualityAsrCheckCorrigendumExplainEntity.setCorrigendumTime(TimeUtil.getLocalDate());
				qualityAsrCheckCorrigendumExplainEntity.setAttachmentId(attId);
				qualityAsrCheckCorrigendumExplainEntity.setCorrigendumExplain("勘误");
				qualityAsrCheckCorrigendumExplainEntity.setCorrigendumUser(baseEntityDataUtil.getCurrentUser());
				qualityAsrCheckCorrigendumExplainEntity.setCorrigendumUserStation(baseEntityDataUtil.getJobNumber());
				baseEntityDataUtil.setBaseEntityData(qualityAsrCheckCorrigendumExplainEntity);
				corrigendumExplainList.add(qualityAsrCheckCorrigendumExplainEntity);
			}
			saveBatch(corrigendumExplainList, corrigendumExplainList.size());
		}
		resultBean.setObject(corrigendumExplainList);
		return resultBean;
	}
	
	// 通过主键获取对应数据
	public QualityAsrCheckCorrigendumExplain selectByCorrigendumExplainId(long id) {
		QualityAsrCheckCorrigendumExplainEntity selectById = qualityAsrCheckCorrigendumExplainDao.selectById(id);
		QualityAttachmentEntity att=qualityAttachmentDao.selectById(selectById.getAttachmentId());
		QualityAsrCheckCorrigendumExplain qualityAsrCheckCorrigendumExplain = new QualityAsrCheckCorrigendumExplain();
		selectById.setCorrigendumName(att.getRealName());
		selectById.setCorrigendumPath(att.getFilePath());
		qualityAsrCheckCorrigendumExplain.setInnerData(selectById);
		return qualityAsrCheckCorrigendumExplain;
	}
	

}
